package
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;

	public class UtilityClass
	{
		public static function CountNumChildInObject( obj : Object):int
		{
			var count : int = -1;
			if( obj == null )
				return count;
			else
			{
				count = 0;
				for ( var key : String in obj )
					count++;
			}
			
			return count;
		}
		
		public static  function isEmpty( obj : Object ):Boolean
		{
			for( var key : String in obj)
			{
				if( key !=  "length") // obj is array
					return false;
			}
			return true;
		}
		
		public static function scaleFromCenter( obj :DisplayObject, sX : Number = 1.0, sY : Number = 1.0 ):void
		{
			var center : Point = new Point( obj.x + obj.width/2, obj.y + obj.height/2 );
			var m :Matrix = obj.transform.matrix;
			m.tx -= center.x;
			m.ty -= center.y;
			m.scale( sX, sY );
			m.tx += center.x;
			m.ty += center.y;
			
			obj.transform.matrix = m;
			
			
		}
		
		/**
		 * 
		 * @param arrSource : "string" client array
		 * @param arrCheck : "string" server array
		 * @return 
		 * 
		 */		
		public static function getElementsNotExistInArray( arrSource : Object, arrCheck : Array ):Array
		{
			var arrResult : Array = new Array();
			var value : String = "";
			for each( value in arrSource  )
			{
				if( arrCheck.indexOf(value) != -1 )
					arrResult.push(value);
			}
			return arrResult;
		}
		
		public static function converToStringArr( intArray : Array ):Array
		{
			var strArr : Array = new Array();
			for each( var value : int in intArray)
				strArr.push( String(value) );
			return strArr;
		}
		
		public static function getFirstKeyInObject( obj : Object ):String
		{
			var key : String = "";
			for ( key in  obj)
				break;
			return key;
		}
		
		public static function ObjectToArrWithKeyAsValue( obj : Object ):Array
		{
			var arr : Array = new Array();
			for( var key : String in obj)
				arr.push( key );
			return arr;
		}
		
		public static function addKeyValueToObject( scrObj : Object, key : String, value : int ):void
		{
			if( scrObj != null )
			{
				if( scrObj.hasOwnProperty(key) )
					scrObj[key] += value;
				else
					scrObj[key] = value;
			}
		}
		
		public  static function createRandomResbyFrame( data : Object, total : int ):Object
		{
			var numtake : int = total/2;
			var i : int = 0;
			var frames : Array = new Array();
			var randNumber : int = 0;
			var getIt : Boolean = false;
			for( i  = 0; i<numtake; i++ )
			{
				getIt = false;
				while( !getIt )
				{
					randNumber = randomRange(0,total - 1);
					if( frames.indexOf(randNumber) == -1 )
						getIt = true;
				}
				frames[i] = randNumber;
			}
			var resArr : Array = new Array();
			resArr[0] = multiplyObj( data, 1/numtake );
			for( i  = 1; i<numtake-1; i++ )
				resArr[i] = copyObj( resArr[0] );
			for( i  = 0; i<numtake-1; i++ )
				data = subObj(data, resArr[i] );
			resArr[numtake - 1] = data;
			var offRes : Object = multiplyObj(resArr[0], 0.3);
			var hadArr : Array = new Array();
			var length : int = numtake%2 == 0 ? numtake : numtake-1;
			for( i  = 0; i<length; i++ )
			{
				getIt = false;
				while( !getIt )
				{
					randNumber = randomRange(0,numtake - 1);
					if( hadArr.indexOf(randNumber) == -1 )
						getIt = true;
				}
				hadArr.push( randNumber );
				if( randNumber%2 == 0 )
					resArr[randNumber] = addObj( resArr[randNumber] , offRes );
				else
					resArr[randNumber] = subObj( resArr[randNumber] , offRes );
			}
			var result : Object = new Object();
			for( i  = 0; i<numtake; i++ )
			{
				result[frames[i]] = resArr[i];
				traceObj( resArr[i] );
			}
			return result;
		}
		
		public  static function randomRange(minNum:Number, maxNum:Number):Number   
		{  
			return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);  
		}
		
		
		public  static function multiplyObj( value : Object, k :Number ):Object
		{
			var tempt : Object = new Object();
			for( var key : String in  value)
			{
				if( key != "length" && value[key] > 0 )
					tempt[key] = int(value[key]*k);
			}
			return tempt;
		}
		
		/**
		 * 
		 * @param value1
		 * @param value1
		 * @return value1 - value2
		 * 
		 */		
		public  static function subObj( value1 : Object, value2 : Object ):Object
		{
			var tempt : Object = copyObj(value1);
			for( var key : String in  value2)
			{
				if( key != "length" && value2[key] > 0 )
					tempt[key] = value1[key]- value2[key];
			}
			return tempt;
		}
		
		/**
		 * 
		 * @param value1
		 * @param value1
		 * @return value1 + value2
		 * 
		 */		
		public  static function addObj( value1 : Object, value2 : Object ):Object
		{
			var tempt : Object = new Object();
			for( var key : String in  value2)
			{
				if( key != "length" && value2[key] > 0 )
					tempt[key] = value1[key] + value2[key];
			}
			return tempt;
		}
		
		public  static function traceObj( value : Object ):void
		{
			for( var key : String in  value)
			{
				if( key != "length" && value[key] > 0 )
					trace( key + ": " + String(value[key]) )
			}
		}
		
		public  static function copyObj( value : Object ):Object
		{
			var tempt : Object = new Object();
			for( var key : String in  value)
			{
				if( key != "length" && value[key] > 0 )
					tempt[key] = value[key];
			}
			return tempt;
		}
		
		/**
		 * 
		 * @param items[itemType] = quantity
		 * @param total
		 * @return result[frame] = { "itemType" : quantity, ... }
		 * 
		 */		
		public  static function createRandomItemsbyFrame( items : Object, total : int ):Object
		{
			var result : Object = new Object();
			if( isEmpty(items) == true)
				return result;
			for( var type : String in items )
			{
				for( var  i : int = 0 ; i < items[type]; i++ )
				{
					var frame : int = randomRange(0, total - 1);
					if( result.hasOwnProperty(frame) )
					{
						result[frame][type] = 1;
					}
					else
					{
						result[frame] = new Object();
						result[frame][type] = 1;
					}
					
				}
			}
			return result;
		}
		
		public static function copyPropertyObject( obj : Object ):Object
		{
			var result : Object = new Object();
			for( var type : String in obj )
				result[ type ] = obj[ type ];
			return result;
		}
	}
}